package srv.app;

import interfaz.InterfazRemota;

import java.net.InetAddress;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;


import servicio.rmi.ObjetoRemoto;

public class Server {
    public void iniciar() {

    	try {
    		System.setProperty ("java.rmi.server.codebase","file:/c:/devel/proyectos/tpo-distribuidas/TPO-CasaCentral/");
    		System.setProperty("java.security.policy","c:/devel/proyectos/tpo-distribuidas/TPO-CasaCentral/rmi.policy");
    		
    		java.rmi.registry.Registry Naming = null;
 	        Naming = LocateRegistry.createRegistry(1099);
    		
    		InterfazRemota ir = new ObjetoRemoto();
            Naming.rebind ("//localhost/ObjetoRemoto", ir);
            System.out.println("Fijado en //localhost/ObjetoRemoto");
            
            verVinculos();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
  
    
    public void verVinculos() {
        try {
      	  String[] vinculos = Naming.list( "" );
     	  System.out.print("Vinculos disponibles en el servidor: ");
     	  System.out.println(InetAddress.getLocalHost().getHostAddress()); 
      	  for ( int i = 0; i < vinculos.length; i++ )
      		  System.out.println( vinculos[i] );
      	} catch (Exception e) { e.printStackTrace(); }
     }
    
    public InterfazRemota conectar()
	{
		InterfazRemota iRemota = null;
		try
		{	
			System.out.println("Conectar1");
			System.setProperty("java.security.policy", "rmi.policy");
			
			System.out.println("Conectar2");
			System.setSecurityManager( new RMISecurityManager() );
	        
			System.out.println("Conectar3");
	        java.rmi.registry.Registry Naming = null;
	        Naming = LocateRegistry.createRegistry(1099);
	        
	        iRemota = (InterfazRemota)Naming.lookup ("//localhost/ObjetoRemoto");
	        
	        
	        

	        

		}catch (Exception e) {

		}
		return iRemota;
	}
    

}
